home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / rotati1a / rotating.frm (.txt) next >
Visual Basic Form  |  1999-09-30  |  8KB  |  215 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Rotating Cube DEMO"
  6.    ClientHeight    =   3195
  7.    ClientLeft      =   60
  8.    ClientTop       =   345
  9.    ClientWidth     =   4680
  10.    DrawWidth       =   3
  11.    FillColor       =   &H00C0C0C0&
  12.    ForeColor       =   &H00FF0000&
  13.    LinkTopic       =   "Form1"
  14.    ScaleHeight     =   573
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   792
  17.    StartUpPosition =   3  'Windows Default
  18.    WindowState     =   2  'Maximized
  19.    Begin VB.PictureBox Picture1 
  20.       BackColor       =   &H00FFFFFF&
  21.       BorderStyle     =   0  'None
  22.       Height          =   1140
  23.       Left            =   -1035
  24.       ScaleHeight     =   76
  25.       ScaleMode       =   3  'Pixel
  26.       ScaleWidth      =   772
  27.       TabIndex        =   0
  28.       Top             =   1440
  29.       Width           =   11580
  30.       Begin VB.Label Label1 
  31.          AutoSize        =   -1  'True
  32.          Caption         =   "Move the mouse towards the edges of the form to adjust rotation and speed"
  33.          BeginProperty Font 
  34.             Name            =   "Tahoma"
  35.             Size            =   12
  36.             Charset         =   161
  37.             Weight          =   700
  38.             Underline       =   0   'False
  39.             Italic          =   0   'False
  40.             Strikethrough   =   0   'False
  41.          EndProperty
  42.          Height          =   300
  43.          Left            =   0
  44.          TabIndex        =   1
  45.          Top             =   0
  46.          Width           =   9135
  47.       End
  48.    End
  49.    Begin VB.Timer Timer1 
  50.       Interval        =   1
  51.       Left            =   3825
  52.       Top             =   2835
  53.    End
  54. Attribute VB_Name = "frmMain"
  55. Attribute VB_GlobalNameSpace = False
  56. Attribute VB_Creatable = False
  57. Attribute VB_PredeclaredId = True
  58. Attribute VB_Exposed = False
  59. 'Rotating Cube DEMO
  60. Private X(8) As Integer
  61. Private y(8) As Integer
  62. 'Integer arrays that hold the actual 2D coordinates of the
  63. '8 corners of the cube.These are the values used to plot
  64. 'the cube on the form after the X,Y,Z coordinates of each cube
  65. 'corner have been converted to 2 dimensinal X and Y coordinates.
  66. Private Const Pi = 3.14159265358979
  67. 'Constant used to convert degrees to radians
  68. Private CenterX As Integer
  69. Private CenterY As Integer
  70. 'The center of the 3 dimensional plane,where it's
  71. 'X=0 , Y=0 , Z=0
  72. Private Const SIZE = 250
  73. 'The length of the cube achmes,therefore also adjusts the overall
  74. 'size.
  75. Private Radius As Integer
  76. 'The radius of the rotation.Each one of the 8 corners of the cube
  77. 'rotates around the vertical Y axis with the same angular speed and radius
  78. 'of rotation.
  79. Private Angle As Integer
  80. 'The value of this variable loops from 0 to 360 and it is passed
  81. 'as an argument to the COS and SIN functions (sine and cosine)
  82. 'that return the changing Z and X coordinates of each corner
  83. 'as the cube rotates around the Y axis
  84. Private CurX As Integer
  85. Private CurY As Integer
  86. 'Variables that hold the current mouse position on the form.
  87. Private CubeCorners(1 To 8, 1 To 3) As Integer
  88. 'The array that holds the X,Y and Z coordinates of the 8 corners
  89. 'of the cube.Here's how the 8 corners are numbered:
  90. '        7___________8
  91. '        /|         /|             |
  92. '       /_|________/ |             |  /
  93. '       |3|       4| |             | /
  94. '       | |        | |             |/
  95. '       | |________|_|       ------|---------------> x(+)
  96. '       | /5       | /6           /|
  97. '       |/_________|/            / |
  98. '      1           2        Z(+)/  |
  99. '                                 \|/ Y(+)
  100. 'The center of the 3D plane is right on the center of the cube.
  101. 'So ,if SIZE the length of one achmes,it's:
  102. 'CenterCube(1,1) = SIZE/2   ' X coordinate of 1st corner
  103. 'CenterCube(1,2) = SIZE/2   ' Y coordinate
  104. 'CenterCube(1,3) = SIZE/2   ' Z coordinate
  105. 'Actually,we only need to give a value for the Y coordinates
  106. 'of each corner since that will never change during the rotation
  107. 'as all corners rotate around the Y axis ,with only their Z and X
  108. 'coordinates changing periodically.
  109. Private Sub Form_Load()
  110. With Picture1
  111. .Width = Label1.Width
  112. .Height = Label1.Height
  113. End With
  114. Picture1.Move ScaleWidth / 2 - Picture1.ScaleWidth / 2, Picture1.Height
  115. CenterX = ScaleWidth / 2
  116. CenterY = ScaleHeight / 2
  117. 'Set the center of the 3D plane to reflect the center of the
  118. 'form.
  119. Angle = 0
  120. Radius = Sqr(2 * (SIZE / 2) ^ 2)
  121. 'Give a value to the radius of the rotation.This is
  122. 'the Pythagorean theorem that returns the length of the
  123. 'hypotenuse of a right triangle as the square root
  124. 'of the sum of the other two sides raised to the 2nd power.
  125. CubeCorners(1, 2) = SIZE / 2
  126. CubeCorners(2, 2) = SIZE / 2
  127. CubeCorners(3, 2) = -SIZE / 2
  128. CubeCorners(4, 2) = -SIZE / 2
  129. CubeCorners(5, 2) = SIZE / 2
  130. CubeCorners(6, 2) = SIZE / 2
  131. CubeCorners(7, 2) = -SIZE / 2
  132. CubeCorners(8, 2) = -SIZE / 2
  133. 'Assign a value to the Y coordinates of each cube.This
  134. 'will never change through out the rotation since the cube
  135. 'rotates around the Y axis.Play around with these if you like
  136. 'but the 3D prism will no longer resemble a cube :)
  137. End Sub
  138. Private Sub DrawCube()
  139. For i = 1 To 8
  140. X(i) = CenterX + CubeCorners(i, 1) + CubeCorners(i, 3) / 8
  141. y(i) = CenterY + CubeCorners(i, 2) + Sgn(CubeCorners(i, 2)) * CubeCorners(i, 3) / 8
  142. 'These two lines contain the algorith that converts the
  143. 'coordinates of a point on the 3D plane (x,y,z) ,into 2
  144. 'dimensional X and Y coordinates that can be used to plot
  145. 'a point on the form.Play around with the 8's and see
  146. 'what happens...
  147. Line (X(3), y(3))-(X(4), y(4))
  148. Line (X(4), y(4))-(X(8), y(8))
  149. Line (X(3), y(3))-(X(7), y(7))
  150. Line (X(7), y(7))-(X(8), y(8))
  151. Line (X(1), y(1))-(X(3), y(3))
  152. Line (X(1), y(1))-(X(2), y(2))
  153. Line (X(5), y(5))-(X(6), y(6))
  154. Line (X(5), y(5))-(X(1), y(1))
  155. Line (X(5), y(5))-(X(7), y(7))
  156. Line (X(6), y(6))-(X(8), y(8))
  157. Line (X(2), y(2))-(X(4), y(4))
  158. Line (X(2), y(2))-(X(6), y(6))
  159. Line (X(1), y(1))-(X(4), y(4))
  160. Line (X(2), y(2))-(X(3), y(3))
  161. Line (X(4), y(4))-(X(8), y(8))
  162. Line (X(3), y(3))-(X(7), y(7))
  163. 'The plotting of the cube onto the form.
  164. 'We have to draw each achmes seperately and then
  165. ' "connect"  the bottom square with the top square.
  166. DoEvents
  167. End Sub
  168. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
  169. CurX = X
  170. CurY = y
  171. 'Store the current position of the mouse cursor into
  172. 'the variable CurX,CurY.
  173. End Sub
  174. Private Sub Timer1_Timer()
  175. Select Case CurX
  176. Case Is > ScaleWidth / 2
  177. Angle = Angle + Abs(CurX - ScaleWidth / 2) / 20
  178. If Angle = 360 Then Angle = 0
  179. Case Else
  180. Angle = Angle - Abs(CurX - ScaleWidth / 2) / 20
  181. If Angle = 0 Then Angle = 360
  182. End Select
  183. 'Change the direction and the angular speed of the rotation
  184. 'according to the position of the mouse cursor.If it's near
  185. 'the left edge of the form then the rotation will be
  186. 'anti-clockwise ,it's near the right edge it will be
  187. 'clockwise. The closer to the center of the form the
  188. 'cursor is,the slower the cube rotates.
  189. 'The angular speed of the rotation is controlled by the
  190. 'pace at which 'Angle' (the value that we pass to the
  191. '(COS and SIN functions) increases or decreases (increases
  192. 'for anti-clockwise rotation and decreases for clockwise
  193. 'rotation).
  194. For i = 1 To 3 Step 2
  195. CubeCorners(i, 3) = Radius * Cos((Angle) * Pi / 180)
  196. CubeCorners(i, 1) = Radius * Sin((Angle) * Pi / 180)
  197. For i = 2 To 4 Step 2
  198. CubeCorners(i, 3) = Radius * Cos((Angle + 2 * 45) * Pi / 180)
  199. CubeCorners(i, 1) = Radius * Sin((Angle + 2 * 45) * Pi / 180)
  200. For i = 5 To 7 Step 2
  201. CubeCorners(i, 3) = Radius * Cos((Angle + 6 * 45) * Pi / 180)
  202. CubeCorners(i, 1) = Radius * Sin((Angle + 6 * 45) * Pi / 180)
  203. For i = 6 To 8 Step 2
  204. CubeCorners(i, 3) = Radius * Cos((Angle + 4 * 45) * Pi / 180)
  205. CubeCorners(i, 1) = Radius * Sin((Angle + 4 * 45) * Pi / 180)
  206. 'Give the new values to the X and Z coordinates of each one
  207. 'of the 8 cube corners by using the COS and SIN mathematical
  208. 'functions.Notice that corners 1 and 3 always have the same
  209. 'X and Z coordinates, as well as 2 and 4, 5 and 7,6 & 8.
  210. 'Take a look at the little scetch on the top of the form
  211. 'to see how this is explained (imagine the cube rotating
  212. 'around the Y axis)
  213. DrawCube
  214. End Sub
  215.